package com.sanyang.pda.ui.manualpicking;

import android.os.Bundle;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.sanyang.pda.adapter.ScatteredAdapter;
import com.sanyang.pda.bean.ScanBean;
import com.sanyang.pda.bean.ScatteredBean;
import com.sanyang.pda.bean.request.EnterToBePickedRequest;
import com.sanyang.pda.bean.request.EntireTrayConfirmRequest;
import com.sanyang.pda.contract.ScatteredContract;
import com.sanyang.pda.dialog.ChooseDetermineDialog;
import com.sanyang.pda.dialog.ScatteredDialog;
import com.sanyang.pda.presenter.ScatteredPresenter;
import com.sanyang.pda.utils.StringConfig;
import com.sanyang.pda.view.ScanButView;
import com.wangshen.base.app.AppActivityKey;
import com.wangshen.base.ui.mvp.base.ui.BaseMvpFragment;
import com.wangshen.projectbase.R;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class ScatteredFragment extends BaseMvpFragment<ScatteredPresenter> implements ScatteredContract.View {
    ScatteredAdapter adapter;
    @BindView(R.id.tv_tray)
    TextView tvTray;
    @BindView(R.id.rv_list)
    RecyclerView rvList;
    @BindView(R.id.bt_add)
    Button btAdd;
    @BindView(R.id.bt_scattered)
    Button btScattered;
    @BindView(R.id.bt_chosen)
    Button btChosen;
    @BindView(R.id.scan)
    ScanButView scan;
    private String entryNo;

    public ScatteredFragment() {
    }

    public ScatteredFragment(String entryNo) {
        this.entryNo = entryNo;
    }

    @Override
    public ScatteredPresenter getPresenter() {
        return new ScatteredPresenter();
    }

    @Override
    public int getViewId() {
        return R.layout.fragment_scattered;
    }

    @Override
    public void initView() {
        adapter = new ScatteredAdapter(R.layout.item_scattered, new ArrayList<>());
        rvList.setLayoutManager(new LinearLayoutManager(mContext));
        rvList.setAdapter(adapter);
    }

    @Override
    public void initLinsenterner() {

    }

    @Override
    public void onResume() {
        super.onResume();
        scan.setScanCallBack(new ScanButView.ScanCallBack() {
            @Override
            public void callBack(ScanBean scanBean) {
                if (scanBean.getType() == 4){
                    tvTray.setText(scanBean.getCode());
                    presenter.disassemblyList(1,entryNo,scanBean.getCode());
                }
            }
        });
        if (!TextUtils.isEmpty(tvTray.getText())){
            presenter.disassemblyList(1,entryNo,tvTray.getText().toString());
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        scan.setScanCallBack(null);
    }

    @Override
    public void getData() {

    }

    @OnClick(R.id.bt_add)
    public void onBtAddClicked() {
        if (TextUtils.isEmpty(tvTray.getText().toString())){
            showMessage(getString(R.string.tray) + getString(R.string.not_be_empty));
            return;
        }
        Bundle bundle=new Bundle();
        bundle.putString(StringConfig.DELIVERYNO,entryNo);
        bundle.putString(StringConfig.PALLET,tvTray.getText().toString());
        startIntent(AppActivityKey.ADDPICKINGACTIVITY,bundle);
    }

    @OnClick(R.id.bt_scattered)
    public void onBtScatteredClicked() {
        ScatteredDialog scatteredDialog=new ScatteredDialog(mContext, new ScatteredDialog.OnScatteredClick() {
            @Override
            public void onConfirm(String tray, String smalllocation, String scatteredlocation) {
                EntireTrayConfirmRequest entireTrayConfirmRequest=new EntireTrayConfirmRequest();
                entireTrayConfirmRequest.setDisassemblyStoragebin(scatteredlocation);
                entireTrayConfirmRequest.setPallet(tray);
                entireTrayConfirmRequest.setStoragebinId(smalllocation);
                presenter.entireTrayConfirm(entireTrayConfirmRequest);
            }
        });
        scatteredDialog.show();
    }

    @OnClick(R.id.bt_chosen)
    public void onBtChosenClicked() {
        if (TextUtils.isEmpty(tvTray.getText().toString())) {
            showMessage(getString(R.string.tray) + getString(R.string.not_be_empty));
            return;
        }
        ChooseDetermineDialog chooseDetermineDialog=new ChooseDetermineDialog(mContext, new ChooseDetermineDialog.OnChooseClick() {
            @Override
            public void onScanResult(String result) {
                EnterToBePickedRequest enterToBePickedRequest = new EnterToBePickedRequest();
                enterToBePickedRequest.setDeliveryNo(entryNo);
                enterToBePickedRequest.setPallet(tvTray.getText().toString());
                enterToBePickedRequest.setCurrentStoragebin(result);
                presenter.enterToBePicked(enterToBePickedRequest);
            }
        });
        chooseDetermineDialog.show();
    }

    @Override
    public void setList(List<ScatteredBean> list) {
        adapter.setNewInstance(list);
    }
}
